<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddIndexRepay extends Migration
{
    protected $table_summaries = "repay_summaries";
    protected $table_records = "repay_records";
    protected $table_moneys = "repay_record_moneys";
    protected $table_months = "repay_record_months";
    protected $table_commons = "repay_record_commons";
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if(Schema::hasTable($this->table_summaries)){
            Schema::table($this->table_summaries, function (Blueprint $table) {
                $table->dropUnique('loan_id');
                $table->dropIndex('trans_id');
                $table->dropIndex('loan_channel');
            });
            if(Schema::hasTable($this->table_records)){
                Schema::table($this->table_records, function (Blueprint $table) {
                    $table->dropIndex('uid');
                    $table->dropIndex('summary_id');
                    $table->dropIndex('bank_cards_id');
                });
            }
            if(Schema::hasTable($this->table_moneys)){
                Schema::table($this->table_moneys, function (Blueprint $table) {
                    $table->dropIndex('uid');
                    $table->dropIndex('summary_id');
                    $table->dropIndex('record_id');
                    $table->dropIndex('bank_cards_id');
                });
            }
            if(Schema::hasTable($this->table_months)){
                Schema::table($this->table_months, function (Blueprint $table) {
                    $table->dropIndex('uid');
                    $table->dropIndex('summary_id');
                    $table->dropIndex('record_id');
                    $table->dropIndex('bank_cards_id');
                });
            }
            if(Schema::hasTable($this->table_commons)){
                Schema::table($this->table_commons, function (Blueprint $table) {
                    $table->dropIndex('uid');
                    $table->dropIndex('summary_id');
                    $table->dropIndex('record_id');
                    $table->dropIndex('bank_cards_id');
                });
            }
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table($this->table_summaries, function (Blueprint $table) {
            $table->unique('loan_id','loan_id');
            $table->index('trans_id','trans_id');
            $table->index('loan_channel','loan_channel');
        });

        Schema::table($this->table_records, function (Blueprint $table) {
            $table->index('uid','uid');
            $table->index('summary_id','summary_id');
            $table->index('bank_cards_id','bank_cards_id');
        });
        Schema::table($this->table_moneys, function (Blueprint $table) {
            $table->index('uid','uid');
            $table->index('summary_id','summary_id');
            $table->index('record_id','record_id');
            $table->index('bank_cards_id','bank_cards_id');
        });
        Schema::table($this->table_months, function (Blueprint $table) {
            $table->index('uid','uid');
            $table->index('summary_id','summary_id');
            $table->index('record_id','record_id');
            $table->index('bank_cards_id','bank_cards_id');
        });
        Schema::table($this->table_commons, function (Blueprint $table) {
            $table->index('uid','uid');
            $table->index('summary_id','summary_id');
            $table->index('record_id','record_id');
            $table->index('bank_cards_id','bank_cards_id');
        });
    }
}
